package com.jiangxjsy.crm.service.impl;

import com.jiangxjsy.core.common.CrmModule;
import com.jiangxjsy.core.enums.FieldEnum;
import com.jiangxjsy.crm.mapper.CrmFieldSortMapper;
import com.jiangxjsy.crm.service.CrmFieldService;
import com.jiangxjsy.crm.service.CrmFieldSortService;
import com.jiangxjsy.crm.support.CrmFieldSortSupport;
import com.jiangxjsy.model.crm.bo.CrmFieldBO;
import com.jiangxjsy.model.crm.entity.CrmFieldSort;
import com.jiangxjsy.model.crm.vo.CrmFieldSortVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/18 16:20
 */

@Service
public class CrmFieldSortServiceImpl implements CrmFieldSortService {
    @Resource
    private CrmFieldSortMapper crmFieldSortMapper;

    @Resource
    private CrmFieldService crmFieldService;

    @Resource
    private CrmFieldSortSupport crmFieldSortSupport;

    /**
     * 查询模块字段列表
     *
     * @param label 模块标识
     * @param userId 用户ID
     * @return 模块字段列表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<CrmFieldSortVO> queryListHead(Integer label, Long userId) {
        Integer number = crmFieldSortMapper.countByUserIdAndLabel(label, userId);
        if (number == 0) {
            saveUserFieldSort(label, userId);
        }
        return crmFieldSortMapper.queryListHead(label, userId);
    }

    /**
     * 保存用户字段排序
     *
     * @param label 模块标识
     * @param userId 用户ID
     */
    @Override
    public void saveUserFieldSort(Integer label, Long userId) {
        List<CrmFieldSort> fieldSortList = queryAllFieldSortList(label,userId);
        Integer ignore = crmFieldSortMapper.insertCrmFieldSortBatch(fieldSortList);
    }

    /**
     * 查询所有字段排序
     *
     * @param label 模块标识
     * @param userId 用户ID
     * @return 所有字段排序
     */
    @Override
    public List<CrmFieldSort> queryAllFieldSortList(Integer label, Long userId) {
        List<CrmFieldBO> crmFieldList = crmFieldService.list(label, false);
        CrmModule crmEnum = CrmModule.parse(label);
        switch (crmEnum) {
            case CUSTOMER:
                crmFieldList.add(new CrmFieldBO("status", "锁定状态", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("dealStatus", "成交状态", FieldEnum.SELECT));
                crmFieldList.add(new CrmFieldBO("lastTime", "最后跟进时间", FieldEnum.DATETIME));
                crmFieldList.add(new CrmFieldBO("lastContent", "最后跟进记录", FieldEnum.TEXTAREA));
                crmFieldList.add(new CrmFieldBO("detailAddress", "详细地址", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("address", "地区定位", FieldEnum.MAP_ADDRESS));
                crmFieldList.add(new CrmFieldBO("poolDay", "距进入公海天数", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("teamMemberIds", "相关团队", FieldEnum.USER));
                break;
            case BUSINESS:
                crmFieldList.add(new CrmFieldBO("typeName", "商机状态组", FieldEnum.SELECT));
                crmFieldList.add(new CrmFieldBO("statusName", "商机阶段", FieldEnum.SELECT));
                crmFieldList.add(new CrmFieldBO("lastTime", "最后跟进时间", FieldEnum.DATETIME));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("teamMemberIds", "相关团队", FieldEnum.USER));
                break;
            case CONTRACT:
                crmFieldList.add(new CrmFieldBO("checkStatus", "审核状态", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("receivedMoney", "已收款金额", FieldEnum.FLOATNUMBER));
                crmFieldList.add(new CrmFieldBO("unreceivedMoney", "未收款金额", FieldEnum.FLOATNUMBER));
                crmFieldList.add(new CrmFieldBO("lastTime", "最后跟进时间", FieldEnum.DATETIME));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("teamMemberIds", "相关团队", FieldEnum.USER));
                break;
            case RECEIVABLES:
                crmFieldList.add(new CrmFieldBO("checkStatus", "审核状态", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("contractMoney", "合同金额", FieldEnum.FLOATNUMBER));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("teamMemberIds", "相关团队", FieldEnum.USER));
                break;
            case RECEIVABLES_PLAN:
                crmFieldList.add(new CrmFieldBO("num", "期数", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("realReceivedMoney", "实际回款金额",FieldEnum.FLOATNUMBER));
                crmFieldList.add(new CrmFieldBO("realReturnDate", "实际回款时间",FieldEnum.DATETIME));
                crmFieldList.add(new CrmFieldBO("unreceivedMoney", "未回款金额",FieldEnum.FLOATNUMBER));
                crmFieldList.add(new CrmFieldBO("receivedStatus", "回款状态",FieldEnum.SELECT));
                break;
            case LEADS:
                crmFieldList.add(new CrmFieldBO("lastTime", "最后跟进时间", FieldEnum.DATE));
                crmFieldList.add(new CrmFieldBO("lastContent", "最后跟进记录", FieldEnum.TEXTAREA));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                break;
            case PRODUCT:
                crmFieldList.add(new CrmFieldBO("status", "是否上下架", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                break;
            case CONTACTS:
                crmFieldList.add(new CrmFieldBO("lastTime", "最后跟进时间", FieldEnum.DATETIME));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("teamMemberIds", "相关团队", FieldEnum.USER));
                break;
            case CUSTOMER_POOL:
                crmFieldList.add(new CrmFieldBO("lastContent", "最后跟进记录", FieldEnum.TEXTAREA));
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                break;
            case INVOICE:
                crmFieldList.add(new CrmFieldBO("ownerUserName", "负责人", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("checkStatus", "审核状态", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("invoiceStatus", "开票状态", FieldEnum.NUMBER));
                crmFieldList.add(new CrmFieldBO("invoiceNumber", "发票号码", FieldEnum.TEXT));
                crmFieldList.add(new CrmFieldBO("realInvoiceDate", "实际开票日期", FieldEnum.DATE));
                crmFieldList.add(new CrmFieldBO("logisticsNumber", "物流单号", FieldEnum.TEXT));
            case RETURN_VISIT:
            default:
                break;
        }
        if (!CrmModule.RECEIVABLES_PLAN.getType().equals(label) && !CrmModule.RETURN_VISIT.getType().equals(label)) {
            crmFieldList.add(new CrmFieldBO("ownerDeptName", "所属部门", FieldEnum.TEXT));
        }
        crmFieldList.add(new CrmFieldBO("updateTime", "更新时间", FieldEnum.DATETIME));
        crmFieldList.add(new CrmFieldBO("createTime", "创建时间", FieldEnum.DATETIME));
        crmFieldList.add(new CrmFieldBO("createUserName", "创建人", FieldEnum.TEXT));

        List<CrmFieldSort> fieldSortList = new ArrayList<>();
        for (int i = 0; i < crmFieldList.size(); i++) {
            CrmFieldSort fieldSort = new CrmFieldSort();
            fieldSort.setFieldId(crmFieldList.get(i).getFieldId());
            fieldSort.setFieldName(crmFieldSortSupport.parseFieldName(crmFieldList.get(i).getFieldName()));
            fieldSort.setName(crmFieldList.get(i).getName());
            fieldSort.setSort((long) i);
            fieldSort.setUserId(userId);
            fieldSort.setStyle(100L);
            fieldSort.setIsHide(0L);
            fieldSort.setLabel(Long.valueOf(label));
            fieldSort.setType(crmFieldList.get(i).getType());
            fieldSortList.add(fieldSort);
        }
        return fieldSortList;
    }
}
